/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.ExhaustFan;
import me.zhengjie.modules.biz.domain.ExhaustFanLog;
import me.zhengjie.modules.biz.domain.Lab;
import me.zhengjie.modules.biz.repository.LabRepository;
import me.zhengjie.modules.biz.service.dto.FumeHoodDto;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.ExhaustFanLogRepository;
import me.zhengjie.modules.biz.service.ExhaustFanLogService;
import me.zhengjie.modules.biz.service.dto.ExhaustFanLogDto;
import me.zhengjie.modules.biz.service.dto.ExhaustFanLogQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.ExhaustFanLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-17
**/
@Service
@RequiredArgsConstructor
public class ExhaustFanLogServiceImpl implements ExhaustFanLogService {

    private final ExhaustFanLogRepository exhaustFanLogRepository;
    private final ExhaustFanLogMapper exhaustFanLogMapper;
    private final LabRepository labRepository;

    @Override
    public Map<String,Object> queryAll(ExhaustFanLogQueryCriteria criteria, Pageable pageable){
        Page<ExhaustFanLog> page = exhaustFanLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<ExhaustFanLogDto> dto = page.map(exhaustFanLogMapper::toDto);
        List<ExhaustFanLogDto> resultDto = dto.getContent();
        for (ExhaustFanLogDto exhaustFanLogDto:resultDto) {
            if (Objects.nonNull(exhaustFanLogDto.getLabId())) {
                Lab lab = labRepository.findById(exhaustFanLogDto.getLabId()).orElseGet(Lab::new);
                if (Objects.nonNull(lab.getId())) {
                    exhaustFanLogDto.setLabName(lab.getName());
                }
            }
            if (Objects.nonNull(exhaustFanLogDto.getExhaustFanId())) {
                ExhaustFanLog exhaustFan = exhaustFanLogRepository.findById(exhaustFanLogDto.getExhaustFanId()).orElseGet(ExhaustFanLog::new);
                if (Objects.nonNull(exhaustFan.getId())) {
                    exhaustFanLogDto.setName(exhaustFan.getName());
                }
            }
        }
        return PageUtil.toPage(dto);
    }

    @Override
    public List<ExhaustFanLogDto> queryAll(ExhaustFanLogQueryCriteria criteria){
        List<ExhaustFanLog> exhaustFanLogList = exhaustFanLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        List<ExhaustFanLogDto> exhaustFanLogDtoList = exhaustFanLogMapper.toDto(exhaustFanLogList);
        for (ExhaustFanLogDto exhaustFanLogDto : exhaustFanLogDtoList) {
            if (Objects.nonNull(exhaustFanLogDto.getLabId())) {
                Lab lab = labRepository.findById(exhaustFanLogDto.getLabId()).orElseGet(Lab::new);
                if (Objects.nonNull(lab.getId())) {
                    exhaustFanLogDto.setLabName(lab.getName());
                }
            }
            if (Objects.nonNull(exhaustFanLogDto.getExhaustFanId())) {
                ExhaustFanLog exhaustFan = exhaustFanLogRepository.findById(exhaustFanLogDto.getExhaustFanId()).orElseGet(ExhaustFanLog::new);
                if (Objects.nonNull(exhaustFan.getId())) {
                    exhaustFanLogDto.setName(exhaustFan.getName());
                }
            }
        }
        return exhaustFanLogDtoList;
    }

    @Override
    @Transactional
    public ExhaustFanLogDto findById(Integer id) {
        ExhaustFanLog exhaustFanLog = exhaustFanLogRepository.findById(id).orElseGet(ExhaustFanLog::new);
        ValidationUtil.isNull(exhaustFanLog.getId(),"ExhaustFanLog","id",id);
        return exhaustFanLogMapper.toDto(exhaustFanLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ExhaustFanLogDto create(ExhaustFanLog resources) {
        return exhaustFanLogMapper.toDto(exhaustFanLogRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ExhaustFanLog resources) {
        ExhaustFanLog exhaustFanLog = exhaustFanLogRepository.findById(resources.getId()).orElseGet(ExhaustFanLog::new);
        ValidationUtil.isNull( exhaustFanLog.getId(),"ExhaustFanLog","id",resources.getId());
        exhaustFanLog.copy(resources);
        exhaustFanLogRepository.save(exhaustFanLog);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            exhaustFanLogRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ExhaustFanLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ExhaustFanLogDto exhaustFanLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", exhaustFanLog.getName());
            map.put("实验室", exhaustFanLog.getLabName());
            map.put("面风速", exhaustFanLog.getSurfaceWindSpeed());
            map.put("排风量", exhaustFanLog.getExhaustAirRate());
            map.put("风量差量", exhaustFanLog.getAirVolumeDifference());
            String states = "";
            switch (exhaustFanLog.getStatus()) {
                case 0:
                    states = "正常";
                    break;
                case 1:
                    states = "故障";
                    break;
            }
            map.put("状态", states);
            map.put("风量信息", exhaustFanLog.getMessage());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
